package cn.lixin.hrm.template;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class TempAuthorInterceptor implements RequestInterceptor {
    private static final String url = "http://localhost:1070/oauth/token?grant_type=client_credentials&scope=hrm&client_id=system&client_secret=1";
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //获取临时授权的token
        String result = HttpUtil.get(url);
        HashMap<String,Object> map = JSON.parseObject(result, HashMap.class);
        //获取返回结果中的token
        String access_token = map.get("access_token").toString();
        System.out.println(access_token);
        //放入feign发起的请求中
        requestTemplate.header("Authorization","bearer "+access_token);
    }
}